package com.tupu.controller.permission;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tupu.common.Result;
import com.tupu.entity.permission.OrganizationInfo;
import com.tupu.service.permission.OrganizationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/api/organization")
public class OrganizationController {

    @Autowired(required = false)
    private OrganizationService organizationService;

    /**
     * 组织页列表展示 分页 名称模糊搜索
     *
     * @param page
     * @param size
     * @param name
     * @return
     */
    @PostMapping("/list")
    public Result<Page<OrganizationInfo>> findByPage(@RequestParam(required = false, defaultValue = "1") Integer page,
                                                     @RequestParam(required = false, defaultValue = "10") Integer size,
                                                     @RequestParam(required = false) String name) {
        try {
            Page<OrganizationInfo> pageInfo = organizationService.pageInfo(page, size, name);
            return Result.ok(pageInfo);
        } catch (Exception e) {
            return Result.errorMessage(e.getMessage());
        }
    }

    /**
     * 组织页列表展示 不分页
     *
     * @return
     */
    @PostMapping("/listAll")
    public Result<List<OrganizationInfo>> findAll() {
        List<OrganizationInfo> organizationInfo = organizationService.getAll();
        return Result.ok(organizationInfo);
    }

    /**
     * 根据id 查询一条数据
     *
     * @param organ_id
     * @return
     */
    @PostMapping("/findOneById")
    public Result<OrganizationInfo> findOneByOrganId(@RequestParam(required = false) Integer organ_id) {

        try {
            OrganizationInfo organization = organizationService.selectById(organ_id);
            return Result.ok(organization);
        } catch (Exception e) {
            return Result.errorMessage(e.getMessage());
        }
    }

    /**
     * 添加组织 排重
     *
     * @param organization
     * @return
     */
    @PostMapping("/add")
    public Result addOrganization(@RequestBody(required = false) OrganizationInfo organization) {
        try {
            organizationService.add(organization);
            return Result.ok("添加成功");
        } catch (Exception e) {
            return Result.errorMessage(e.getMessage());
        }
    }

    /**
     * 根据organ_id 删除组织（可批量删除）
     *
     * @param ids
     * @return
     */
    @PostMapping("/del")
    public Result delOrganization(@RequestParam(required = false) List<Integer> ids) {
        try {
            organizationService.delByIds(ids);
            return Result.ok("删除成功");
        } catch (Exception e) {
            return Result.errorMessage(e.getMessage());
        }
    }

    /**
     * 根据id 更新组织
     *
     * @param organization
     * @return
     */
    @PostMapping("/update")
    public Result updateOrganization(@RequestBody(required = false) OrganizationInfo organization) {
        try {
            Integer result = organizationService.updateOrgan(organization);
            if (result == 1) {
                return Result.ok("修改成功");
            } else {
                return Result.ok("组织已存在");
            }
        } catch (Exception e) {
            return Result.errorMessage(e.getMessage());
        }
    }

}